<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <!-- <el-form-item>
          <el-popover
            placement="right"
            title="选择商家"
            trigger="click"
            width="800"
            hight="1000"
            v-model="merchantVisible">
            <el-row>
              <el-form-item  prop="pay_type"  style="float:left;padding-top:10px;padding-left:15px;">
                <el-input v-model="filtersCyTable1.merchant_code" placeholder="商家编码"></el-input>
              </el-form-item>
              <el-form-item lable="商家名称"  style="float:left;padding-top:10px;padding-left:15px;">
                <el-input v-model="filtersCyTable1.merchant_name" placeholder="商家名称"></el-input>
              </el-form-item>
              <el-form-item style="float:left;padding-top:10px;padding-left:15px;">
                <kt-button icon="fa fa-search" :label="$t('action.search')" type="primary"
                           @click="resetCyTable1"/>
              </el-form-item>
              <el-form-item style="float:left;padding-top:10px;padding-left:15px;">
                <kt-button icon="fa fa-refresh" :label="$t('action.reset')"
                           @click="resetCyTable1"
                           type="primary"/>
              </el-form-item>

              <el-form-item style="float:left;padding-top:10px;padding-left:15px;">
                <kt-button icon="fa fa-hand-pointer-o" :label="$t('action.comfirm')"
                           @click="operationClick"
                           type="primary"/>
              </el-form-item>
            </el-row>

            <cy-table1 :height="1000"
                       :data="pageResult" :columns="clickColumns"
                       @findPage="clinkMock" ref="CyTable1"
                       :showBatchDelete="false" :showDeleteOperation=false
                       :showEditOperation=false
                       :showDetailOperation=false :showOperation="false">
            </cy-table1>

            <el-input slot="reference" v-model="filters.merchant_name"
                      placeholder="商家名称" :disabled="true"></el-input>
          </el-popover>
        </el-form-item>-->
        <el-row class="search-input">
          <el-form-item label="支付方式：" prop="pay_type">
          <el-input v-model="filters.methodName" placeholder="支付方式"></el-input>
        </el-form-item>
        <el-form-item label="业务类型：" prop="business_type">
          <el-select v-model="filters.businessType" placeholder="业务类型">
            <el-option label="请选择" value=""></el-option>
            <el-option
              v-for="item in businessTypeOption"
              :key="item.code"
              :label="item.label"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="销售渠道：" prop="sale_channel">
          <el-select v-model="filters.saleChannelCode" placeholder="销售渠道">
            <el-option label="请选择" value=""></el-option>
            <el-option
              v-for="item in saleChannelOption"
              :key="item.channelCode"
              :label="item.channelName"
              :value="item.channelCode"
            ></el-option>
          </el-select>
        </el-form-item>
        </el-row>
        <el-row class="search-button">
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
            <kt-button
              icon="fa fa-plus"
              :label="$t('action.add')"
              type="primary"
              @click="handleAdd"
            />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-refresh"
              :label="$t('action.reset')"
              type="primary"
              @click="resetForm('filters')"
            />
          </el-form-item>
        </el-row>
        <el-row class="t_r button">
           <el-form-item>
            <el-button-group>
              <el-tooltip content="刷新" placement="top">
                <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
              </el-tooltip>
              <el-tooltip content="列显示" placement="top">
                <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
              </el-tooltip>
              <el-tooltip content="导出" placement="top">
                <el-button icon="fa fa-file-excel-o" @click="downloadExcel"></el-button>
              </el-tooltip>
            </el-button-group>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-right:15px;">
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :width="1000"
      :height="250"
      :data="pageResult"
      :columns="filterColumns"
      :showEditOperation="true"
      :showBatchDelete="true"
      :showDeleteOperation="true"
      @findPage="findPage"
      @handleEdit="handleEdit"
      @handleDelete="handleDelete"
      ref="CyTable"
    ></cy-table>
    <!--新增编辑界面-->
    <el-dialog
      :title="operation?'新增':'编辑'"
      width="40%"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="100px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
        label-position="right"
      >
        <el-row class="searchDialog-input">
           <el-form-item label="ID：" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off" placeholder="请输入ID"></el-input>
        </el-form-item>
        <el-form-item label="支付方式：" prop="methodName" class="w308">
          <el-input v-model="dataForm.methodName" placeholder="支付方式"></el-input>
        </el-form-item>
        <el-form-item label="业务类型：" prop="businessType">
          <el-select v-model="dataForm.businessType" placeholder="业务类型">
            <el-option label="请选择" value=""></el-option>
            <el-option
              v-for="item in businessTypeOption"
              :key="item.code"
              :label="item.label"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="销售渠道：" prop="saleChannelCode">
          <el-select v-model="dataForm.saleChannelCode" placeholder="销售渠道">
            <el-option label="请选择" value=""></el-option>
            <el-option
              v-for="item in saleChannelOption"
              :key="item.channelCode"
              :label="item.channelName"
              :value="item.channelCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态：" prop="status">
          <el-radio v-model="radio" label="1">启用</el-radio>
          <el-radio v-model="radio" label="2">禁用</el-radio>
        </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="dialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >{{$t('action.submit')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PopupTreeInput from "@/components/PopupTreeInput";
import CyTable from "@/views/Core/JicTable";
import CyTable1 from "@/views/Core/CyTable";
import KtButton from "@/views/Core/KtButton";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
export default {
  components: {
    PopupTreeInput,
    CyTable,
    CyTable1,
    KtButton,
    TableColumnFilterDialog
  },
  data() {
    return {
      size: "small",
      filters: {
        merchant_name: "",
        methodName: "",
        businessType: "",
        saleChannelCode: ""
      },
      businessTypeOption: [
        {
          code: "1",
          label: "普通支付"
        },
        {
          code: "2",
          label: "充值"
        }
      ],
      saleChannelOption: [],
      radio: "1",
      options: {},
      filtersCyTable1: {
        merchant_code: "",
        merchant_name: ""
      },
      columns: [
        { prop: "methodName", label: "支付方式", minWidth: 100 },
        { prop: "level", label: "支付方式级别", minWidth: 100 },
        {
          prop: "businessType",
          label: "业务类型",
          minWidth: 100,
          formatter: this.showBusinessType
        },
        {
          prop: "saleChannelCode",
          label: "销售渠道",
          minWidth: 100,
          formatter: this.showSaleChannelCode
        },
        {
          prop: "status",
          label: "状态",
          minWidth: 100,
          formatter: this.showStatusType
        },
        { prop: "creatorName", label: "创建人", minWidth: 100 },
        { prop: "createTime", label: "创建时间", minWidth: 100 },
        { prop: "updaterName", label: "更新人", minWidth: 100 },
        { prop: "updateTime", label: "更新时间", minWidth: 100 }
      ],
      clickColumns: [
        { prop: "merchantId", label: "商家编码", minWidth: 120 },
        { prop: "merchantName", label: "商家名称", minWidth: 120 },
        { prop: "merchantType", label: "商家类型", minWidth: 120 }
      ],
      mockPage: {
        total: "3",
        rows: [
          {
            id: "1",
            pay_type: "在线支付",
            pay_type_level: "1",
            business_type: "普通支付",
            sale_channel: "自建B2C",
            merchant_id: "1212334",
            shop_id: "154",
            status: "启用",
            create_por: "merchant",
            create_time: "2019-11-29",
            edit_por: "superadmin",
            edit_time: "2019-12-02"
          },
          {
            id: "2",
            pay_type: "在线支付",
            pay_type_level: "1",
            business_type: "充值",
            sale_channel: "天猫",
            merchant_id: "1212334",
            shop_id: "154",
            status: "启用",
            create_por: "merchant",
            create_time: "2019-11-29",
            edit_por: "superadmin",
            edit_time: "2019-12-02"
          },
          {
            id: "3",
            pay_type: "在线支付",
            pay_type_level: "1",
            business_type: "普通支付",
            sale_channel: "美团",
            merchant_id: "1212334",
            shop_id: "154",
            status: "启用",
            create_por: "merchant",
            create_time: "2019-11-29",
            edit_por: "superadmin",
            edit_time: "2019-12-02"
          }
        ]
      },
      clickDetail: {
        rows: [
          {
            merchantId: "300001",
            merchantName: "录屏商家二",
            merchantType: "自营商家"
          },
          {
            merchantId: "300001",
            merchantName: "录屏商家二",
            merchantType: "自营商家"
          }
        ],
        total: 1,
        code: "0000"
      },

      filterColumns: [],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: {},

      operation: false, // true:新增, false:编辑
      dialogVisible: false,
      detailDialogVisible: false, // 新增编辑界面是否显示
      merchantVisible: false,
      editLoading: false,
      // 新增编辑界面数据
      dataForm: {
        pay_type: "",
        business_type: "",
        sale_channel: "",
        status: "1"
      },
      dataFormRules: {
        pay_type: [
          { required: true, message: "请输入支付方式", trigger: "blur" }
        ],
        business_type: [
          { required: true, message: "请输入业务类型", trigger: "blur" }
        ],
        sale_channel: [
          { required: true, message: "请输入销售渠道", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 获取分页数据
    findPage: function(data) {
      let this_ = this;
      if (this_.filters == undefined || this_.filters == null) {
        this_.filters = {};
      }
      this_.filters.start = this.pageRequest.pageNum;
      this_.filters.limit = this.pageRequest.pageSize;
      this.utils.request.queryPaymentMethodList(this_.filters, function(res) {
        if (res.data.rows == null) {
          res.data.rows = [];
        }
        this_.$refs.CyTable.findPage({
          content: res.data.rows,
          total: res.data.total
        });
      });
    },
    queryChannelListOther: function() {
      var this_ = this;
      this.utils.request.queryChannelListOther({}, function(res) {
        if (res.data == null) {
          res.data = [];
        }
        this_.saleChannelOption = res.data;
      });
    },
    //列表业务类型处理
    showBusinessType(row, column, cellValue, index) {
      if (row.businessType == 1) {
        return "普通支付";
      } else if (row.businessType == 2) {
        return "充值";
      }
    },
    //列表业务类型处理
    showSaleChannelCode(row, column, cellValue, index) {
      var obj = this.saleChannelOption;
      for (var i = 0; i < obj.length; i++) {
        if (obj[i].channelCode == row.saleChannelCode) {
          return obj[i].channelName;
        }
      }
    },
    //列表状态处理
    showStatusType(row, column, cellValue, index) {
      if (row.status == 1) {
        return "启用";
      } else if (row.status == 2) {
        return "禁用";
      }
    },
    clinkMock: function() {
      this.$refs.CyTable1.findPageMock(this.clickDetail);
    },
    operationClick: function() {
      var this_ = this;
      this_.filters.merchant_name = "录屏商家二";
      this_.merchantVisible = false;
    },
    resetCyTable1: function() {
      this.filtersCyTable1 = {};
      this.clinkMock();
    },

    resetForm(formName) {
      if (this.$refs[formName] != undefined) {
        this.$refs[formName].resetFields();
        this.filters = {};
        this.findPage(null);
      }
    },
    // 编辑
    submitForm: function() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            let params = Object.assign({}, this.dataForm);
            var this_ = this;
            params.status = this.radio;
            this.utils.request.editPaymentMethod(params, function(res) {
              if (res.code == "0000") {
                this_.$message({ message: "操作成功", type: "success" });

                this_.$refs["dataForm"].resetFields();

                this_.dialogVisible = false;

                this_.findPage(null);
              } else {
                this_.$message({
                  message: "操作失败, " + res.msg,
                  type: "error"
                });
              }
            });
          });
        }
      });
    },
    // 显示新增界面
    handleAdd: function() {
      var this_ = this;
      this_.resetForm("dataForm");
      this_.dialogVisible = true;
      this_.operation = true;
    },
    // 显示编辑界面
    handleEdit: function(params) {
      console.log(params);
      this.dialogVisible = true;
      this.operation = false;
      this.dataForm = Object.assign({}, params.row);
    },

    // 批量删除
    handleDelete: function(data) {
      if (data != null && data.params != null && data.params.length > 0) {
        let ids = data.params.map(item => item.id).toString();
        console.log(ids);
        var params = {};
        params.ids = ids;
        var this_ = this;
        this.utils.request.deletePaymentMethodInfo(params, function(res) {
          if (res.code == "0000") {
            this_.$message({ message: "操作成功", type: "success" });
            this_.findPage(null);
          } else {
            this_.$message({ message: "操作失败, " + res.msg, type: "error" });
          }
        });
      }
    },
    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      var temp = [];

      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    //列表下载
    downloadExcel() {
      this.$confirm("确定下载列表文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let filename = "系统参数下载";
          this.$refs.CyTable.exportData(
            this.filters,
            this.filterColumns,
            filename
          );
        })
        .catch(() => {});
    }
  },

  mounted() {
    this.initColumns();
    this.queryChannelListOther();
  }
};
</script>

<style scoped lang='scss'>
.w308{
  width: 308px;
}
</style>
